package net.mikoo.seals.boss.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.boss.biz.services.AgentServices;
import net.mikoo.seals.boss.common.BaseController;
import net.mikoo.seals.boss.common.PageModel;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.model.Agent;
import net.mikoo.seals.boss.model.AgentQuery;

@Controller
@RequestMapping("/admin/agent")
public class AgentController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(AgentController.class);
	
	@Autowired
	private AgentServices agentServices;
	
	
	@RequestMapping("/list")
	public String list(AgentQuery query, HttpServletRequest request, Model model) {
		
		PageModel<Agent> pm  = agentServices.loadByQueryToPage(query);
		model.addAttribute("pm", pm);
		model.addAttribute("query", query);
		
		return resultView(ADMIN_VIEW, "agent/list");
	}
	

	@RequestMapping("/addInfo")
	public String addInfo() {
		
		return resultView(ADMIN_VIEW, "agent/addInfo");
	}
	
	
	
	@RequestMapping("/create")
	@ResponseBody
	public Map<String,Object> create(Agent agent,HttpServletRequest request, Model model) {
		try {
			agentServices.save(agent);
			
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
	}
	
	/**
	 * 公司详情
	 * @param companyId
	 * @param model
	 * @return
	 */
	@RequestMapping("/info")
	public String info(Long agentId, Model model) {
		Agent agent = agentServices.loadByPrimaryKey(agentId);
		model.addAttribute("agent", agent);
		
		return resultView(ADMIN_VIEW, "agent/info");
	}
	
	

	@RequestMapping("/edit")
	public String edit(Long agentId, Model model) {
		Agent agent = agentServices.loadByPrimaryKey(agentId);
		model.addAttribute("agent", agent);
		return resultView(ADMIN_VIEW, "agent/edit");
	}
	
	
	@RequestMapping("/modify")
	public Map<String,Object> modify(Agent agent, Model model) {
		
		try {
			agentServices.modifyByPrimaryKey(agent);
			return result(SUCCESS, "添加成功");
		}catch (ParametersException e) {
			logger.error("",e);
			return result(ERROR, e.getMessage());
		}catch (Exception e) {
			logger.error("",e);
			return result(ERROR, "系统出错");
		}
		
	}
	

}
